import java.util.*
// import kotlin.collections.ArrayList
import kotlin.collections.toIntArray as kotlinCollectionsToIntArray

/*
解题思路:
条件3是多余的
按年龄顺序排序有助于过滤条件2
条件1适合于二分搜索
年龄相同的情况下，需要考虑条件1
 */
/* age[B] <= 0.5 * age[A] + 7 */
/* age[B] > age[A] */
/* age[B] > 100 && age[A] < 100 */
class Solution {
    fun numFriendRequests(ages: IntArray): Int {
        ages.sort()
        var ans = 0
        // for( i in 0 until ages.size)
        for( i in ages.indices){
            var target = 0.5 * ages[i] + 7
            var l = 0
            var r = i -1
            var m = l
            while(l <= r){
                m = l + r shr 1
                when{
                    ages[m] <= target -> {
                        l = m + 1
                    }
                    ages[m] > target -> {
                        r = m - 1
                    }
                }
            }
            ans += i - l
            // println("$i $l $target")
            if(ages[i] <= target){
                continue
            }
            l = i
            r = ages.size -1
            m = l
            while(l <= r){
                m = l + r shr 1
                when{
                    ages[m] <= ages[i] -> {
                        l = m + 1
                    }
                    ages[m] > ages[i] -> {
                        r = m - 1
                    }
                }
            }
            // println("$i $l $target")
            ans += l - i -1
        }
        return ans

    }
}


fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    val str = scanner.nextLine()
    // 加 "str:", 是报错的
    println(str)
    // 输入2
    val agesInput = readLine()
    println(agesInput)
    // val result = "[1,2,3,4,5]".removeSurrounding("[", "]").split(",").map { it.toInt() }
    // val result = "[1, 2, 3, 4, 5]".removeSurrounding("[","]").replace(" ","").split(",").map { it.toInt() }
    // "[1,2,3,4,5]".replace(Regex("""[\[,\]]"""), "").map { it - '0' }
    val ages = agesInput?.removeSurrounding(
        "[",
        "]"
    )?.split(",")?.map { it.toInt()}
    if (ages != null) {
        // [I@63961c42
        println(ages.kotlinCollectionsToIntArray())
        println(Solution().numFriendRequests(ages.kotlinCollectionsToIntArray()))
    }
    // ages: IntArray
    // int类型的数组类型
    // val intArray :IntArray =intArrayOf(0,1,2,3,4)
    // String类型的集合 // 加:ArrayList是报错的
    // val stringArray = arrayListOf("一斤","二斤","三斤","四斤")
    // 方法3：最笨的 "[16,16]"
    val intArray :IntArray =intArrayOf(16, 16)
    println(Solution().numFriendRequests(intArray))
}
